(function () {
  new Vue({
    el: '#costItemUnit',
    data() {
      return {
        unitList: [],
        isNewAdd: false, //新增编辑栏
        newValue: '',
      }
    },
    mounted() {
      this.getUnitList();
    },
    methods: {
      //获取计量单位
      getUnitList() {
        var that = this
        hlhttp.get({
          url: 'configure/dict',
          data: {
            module_code: 'ZHSH',
            config_name: 'ZS_LOSS_REASON'
          },
          success: function (data) {
            data.map(item => {
              item.isEdit = false
            })
            that.unitList = data
          },
        })
      },
      // 确认编辑
      sureEdit(item) {
        var that = this;
        hlhttp.put({
          url: 'configure/user_defined_dict',
          data: {
            "module_code": "ZHSH",
            "config_name": "ZS_LOSS_REASON",
            "dict_code": item.dictCode,
            "value": item.dictName
          },
          success: function (res) {
            item.isEdit = false
          }
        })
      },
      // 启用或者停用
      switchFunc(item) {
        var that = this;
        hlhttp.put({
          url: 'configure/user_defined_dict/enable_and_disable',
          data: {
            "module_code": "ZHSH",
            "config_name": "ZS_LOSS_REASON",
            "dict_code": item.dictCode,
            "status": item.status == 'Y'?'N':'Y'
          },
          success: function (res) {
            item.isEdit = false
            that.getUnitList()
          }
        })
      },
      // 删除
      deleteItem(item) {
        var that = this;
        hlhttp.delete({
          url: 'configure/user_defined_dict?module_code=ZHSH&config_name=ZS_LOSS_REASON&dict_code='+item.dictCode,
          data: {
            "module_code": "ZHSH",
            "config_name": "ZS_LOSS_REASON",
            "dict_code": item.dictCode,
          },
          success: function (res) {
            hl.toast('删除成功');
            that.getUnitList()
          }
        })
      },
      newAddBut() {
        for (let n = 0; n < this.unitList.length; n++) {
          if (this.unitList[n].isEdit) {
            hl.toast('请先保存')
            return
          }
        }
        this.isNewAdd = true
        this.newValue = ''
      },
      // 新增
      newAdd() {
        if(!this.newValue){
          hl.toast('请输入名称')
          return
        }
        var that = this;
        hlhttp.post({
          url: 'configure/user_defined_dict',
          data: {
            "module_code": "ZHSH",
            "config_name": "ZS_LOSS_REASON",
            "parent_code": "#",
            "value":this.newValue
          },
          success: function (res) {
            hl.toast('添加成功');
            that.isNewAdd = false
            that.getUnitList()
          }
        })
      }
    },
  })
}())
